package com.ciwong.epaper.modules.pad.homeworkwidget.presenter;

import android.content.Context;
import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.ui.clazz.ChangeClassActivity;
import com.ciwong.epaper.modules.pad.contentprovider.LoginToken;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.i.BaseCallBack;

import java.io.Serializable;
import java.util.List;

import static com.ciwong.mobilelib.utils.BaseJumpManager.GO_BACK_ID;
import static com.ciwong.mobilelib.utils.BaseJumpManager.INTENT_FLAG_SOURCE;

public class HomeworkWidgetCheckState {

    private static String TAG = "HomeworkWidgetCheckState";

    /**
     * 判断是否登录成功
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        boolean isLogin = false;
        LoginToken loginToken = LoginToken.queryTokenAndUserInfo(context.getContentResolver());
        if (loginToken != null) {
            isLogin = true;
        }
        return isLogin;
    }
    /**
     * 判断是否绑定定电话成功
     *
     * @return
     */
    public static boolean isBindPhone() {
        boolean isBindPhone;
        String userPhone=null;
        try {
            userPhone = SerializableManager.getInstance().getUserPhone(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (userPhone == null || userPhone.equals("")) {
            isBindPhone= false;
        }else{
            isBindPhone=true;
        }
        return isBindPhone ;

    }

    /**
     * 是否加入班级本地判断
     *
     * @return
     */
    public static boolean isJoinClass() {

        boolean isJoinClass = false;
        Clazz clazz = EApplication.getInstance().getClazz();
        if (clazz != null) {
            isJoinClass = true;
        }
        return isJoinClass;
    }
    private static long oldTime=0;
    private static long newTime=0;
    private static long duration=1*1000;
    public static void checkNetIsJoinClass(final Context context) {
        newTime = System.currentTimeMillis();
        if (newTime-oldTime<duration){//控制请求次数
            return;
        }
        oldTime= newTime;
        MeDao.getInstance().getClassList(EApplication.BRAND_ID, new BaseCallBack() {
                    @Override
                    public void success(Object data) {
                        final List<Clazz> clazzs = (List<Clazz>) data;
                        Clazz clazz = EApplication.getInstance().getClazz();
                        if (clazzs == null || clazzs.isEmpty()) {
                            clazz = null;
                        } else {
                            int indexOf = clazzs.indexOf(clazz);
                            if (indexOf < 0) {
                                indexOf = 0;
                            }
                            clazz = clazzs.get(indexOf);
                        }
                        if (clazz!=null){
                            context.sendBroadcast(new Intent(AppWidgetConstants.ACTION_HAS_JOINED_CLASS));
                        }
                        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CLASS_DEFAULT, clazz);
                        SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CLASS_LIST, (Serializable) clazzs);
                        EApplication.getInstance().setClazzs(clazzs);
                        EApplication.getInstance().setClazz(clazz);

                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        CWLog.d(TAG, "拉取班级列表失败:" + errorCode);
                        failed(data);
                    }

                    @Override
                    public void failed(Object data) {
                        CWLog.d(TAG, "拉取班级列表失败:" + data);
//                        goToJoinInClass();
                    }

                });
    }
    /**
     * 是否加入班级网络判断
     *
     * @return
     */
    public static void clickCheckNetIsJoinClass( Context context) {
        goToJoinInClass(context);
     /*   MeDao.getInstance().getClassList(EApplication.BRAND_ID, new BaseCallBack() {
            @Override
            public void success(Object data) {
                final List<Clazz> clazzs = (List<Clazz>) data;
                Clazz clazz = EApplication.getInstance().getClazz();
                if (clazzs == null || clazzs.isEmpty()) {
                    clazz = null;
                } else {
                    int indexOf = clazzs.indexOf(clazz);
                    if (indexOf < 0) {
                        indexOf = 0;
                    }
                    clazz = clazzs.get(indexOf);
                }
                if (clazz!=null){
                    context.sendBroadcast(new Intent(AppWidgetConstants.ACTION_HAS_JOINED_CLASS));
                } else{
                    goToJoinInClass();
                }
                SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CLASS_DEFAULT, clazz);
                SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_CLASS_LIST, (Serializable) clazzs);
                EApplication.getInstance().setClazzs(clazzs);
                EApplication.getInstance().setClazz(clazz);

            }

            @Override
            public void failed(int errorCode, Object data) {
                CWLog.d(TAG, "拉取班级列表失败:" + errorCode);
                failed(data);
            }

            @Override
            public void failed(Object data) {
                CWLog.d(TAG, "拉取班级列表失败:" + data);
                goToJoinInClass();
            }

            private void goToJoinInClass() {
                Intent newIntent = new Intent(context, ChangeClassActivity.class);
                newIntent.putExtra(INTENT_FLAG_SOURCE, true);
                newIntent.putExtra(GO_BACK_ID, R.string.go_back);
                newIntent.putExtra(IntentFlag.INTENT_FLAG_TYPE, IntentValue.JumpToJoinClassType.TYPE_JOIN_CLASS);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(newIntent);

            }
        });*/
    }
    private static void goToJoinInClass(Context context) {
        Intent newIntent = new Intent(context, ChangeClassActivity.class);
        newIntent.putExtra(INTENT_FLAG_SOURCE, true);
        newIntent.putExtra(GO_BACK_ID, R.string.go_back);
        newIntent.putExtra(IntentFlag.INTENT_FLAG_TYPE, IntentValue.JumpToJoinClassType.TYPE_JOIN_CLASS);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(newIntent);

    }
}
